package com.example.review.Fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.review.Entity.Carlist;
import com.example.review.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExpandFragment extends Fragment {
    private View view;
    private ExpandableListView expand;

    public ExpandFragment() {
        // Required empty public constructor
    }

    private interface Blogserver{
        @GET("carlist")
        Call<ResponseBody> getBlog();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_expand, container, false);
        initView();
        initData();
        return view;
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Blogserver blogserver = retrofit.create(Blogserver.class);
        Call<ResponseBody> blog = blogserver.getBlog();
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    List<Carlist> list = new ArrayList<>();
                    String string = response.body().string();
                    Carlist carlist = new Gson().fromJson(string, Carlist.class);
                    list.add(carlist);
                    list.add(carlist);
                    list.add(carlist);
                    expand.setAdapter(new expendAdapter(list));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {
        expand = view.findViewById(R.id.expand);
    }

    private class expendAdapter extends BaseExpandableListAdapter {
        List<String> list=new ArrayList<>();
        List<Carlist> carlists;

        public expendAdapter(List<Carlist> carlists) {
            for (int i = 0; i < carlists.size(); i++) {
                list.add("第" + i + "个");
            }
            this.carlists = carlists;
        }

        @Override
        public int getGroupCount() {
            return list.size();
        }

        @Override
        public int getChildrenCount(int i) {
            return carlists.size();
        }

        @Override
        public Object getGroup(int i) {
            return list.get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return carlists.get(i).getData().get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }
        public class ViewHolder {
            public View rootView;
            public TextView expand_text1;
            public TextView expand_text2;
            public TextView expand_text3;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.expand_text1 = (TextView) rootView.findViewById(R.id.expand_text1);
                this.expand_text2 = (TextView) rootView.findViewById(R.id.expand_text2);
                this.expand_text3 = (TextView) rootView.findViewById(R.id.expand_text3);
            }

        }
        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.expand_layout, viewGroup, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            holder.expand_text1.setText(list.get(i));
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if(view==null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.expand_layout, viewGroup, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
            }
            holder.expand_text1.setText(carlists.get(i).getData().get(i1).getCarno());
            holder.expand_text2.setText(carlists.get(i).getData().get(i1).getWho());
            holder.expand_text3.setText(carlists.get(i).getData().get(i1).getAmount()+"");
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }

    }
}
